package com.zephyr.vchcenter.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;

import com.zephyr.vchcenter.bean.Voucher;
import com.zephyr.vchcenter.dao.IBaseDao;
import com.zephyr.vchcenter.service.IIdentifyService;
import com.zephyr.vchcenter.service.IImageService;
import com.zephyr.vchcenter.service.IStampService;
import com.zephyr.vchcenter.util.XmlParseUtil;

public class IdentifyServiceImpl implements IIdentifyService {

	private static Logger log = Logger.getLogger(IdentifyServiceImpl.class);

	private IBaseDao baseDAO;

	public void setBaseDAO(IBaseDao baseDAO) {
		this.baseDAO = baseDAO;
	}

	public String getIdentifyXML(String vchsid, String operateip,
			String vchdate, String amount, int sceneflag) {

		Voucher voucher = (Voucher) baseDAO.findById("Voucher", vchsid);
		log.debug("vchsid: " + vchsid + " ,voucher: " + voucher.toString());

		// 1、获取图片
		IImageService iis = new ImageServiceImpl();

		// String imgXML =
		// XmlParseUtil.markImageServXML(voucher.getFimgid(),"");
		// String image = iis.getImage(2202,imgXML);
		String image = iis.getLocalImage(IdentifyServiceImpl.class.getResource(
				"/00000001.jpg").getPath());
		log.debug("获取图片服务返回信息： " + image.substring(0, 100));
		// 2、获取印件库信息
		String tmpAmount = "";
		String tmpVchdate = "";
		if (!"".equals(amount)) {// 如果金额不为空，使用参数中的金额，否则使用票据对象中的金额
			tmpAmount = amount;
			voucher.setAmount(amount);
			baseDAO.saveOrUpdate(voucher);//存储修改的金额
		} else {
			tmpAmount = voucher.getAmount();
		}
		if ("0".equals(vchdate)) {
			tmpVchdate = "";
		} else if ("1".equals(vchdate)) {
			tmpVchdate = voucher.getVchdate();
		} else {
			tmpVchdate = vchdate;
			voucher.setVchdate(vchdate);
			baseDAO.saveOrUpdate(voucher);//存储修改的出票日期
		}
		
		String stampXml = XmlParseUtil.mark2003XML(operateip, voucher, "0",
				tmpVchdate, tmpAmount);

		IStampService iss = new StampServiceImpl();
		String RestampXML = iss.getStamp(2003, stampXml);
		log.debug("获取印件库服务返回信息： " + RestampXML.substring(0, 100));

		String resVerify = XmlParseUtil.verifyStampXML(RestampXML);
		if (!"".equals(resVerify)) {
			return resVerify;
		}
		String zkernel = null;
		String seal = null;
		try {
			if (RestampXML != null && !RestampXML.equals("")) {
				zkernel = XmlParseUtil.getNodeXML(RestampXML, "zkernel");
				seal = XmlParseUtil.getNodeXML(RestampXML, "seal");
			} else {
				log.debug("RestampXML is null!");
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		// 3、组织交易报文
		String tradeXml = null;
		try {
			tradeXml = XmlParseUtil.makeTradeDom4jXml(voucher, zkernel, seal,
					image, sceneflag);
			log.debug("makeTradeXml is: " + tradeXml);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tradeXml;
	}

	public String decodeResultXML(int comid, String xml, String vchsid) {
		Voucher voucher = (Voucher) baseDAO.findById("Voucher", vchsid);
		log.debug("vchsid: " + vchsid + " ,voucher: " + voucher.toString());
		log.debug("XML: " + xml + " ,cmid: " + comid);
		if (comid == 1006) { // 人工干预使用
			try {
				voucher = XmlParseUtil.parse1006XML(voucher, xml);
			} catch (DocumentException e) {
				log.debug(e.getMessage());
			}
		} else if (comid == 1015) { // 本地扫描使用
			List list = null;
			try {
				list = XmlParseUtil.parse1015XML(voucher, xml);
			} catch (DocumentException e) {
				log.debug(e.getMessage());
			}
			if (list == null) {
				return "failure,list is null.";
			}
			voucher = (Voucher) list.get(0);
			String vouImg = (String) list.get(1);
			if (vouImg != null || !"".equals(vouImg)) {
				log.debug("存储新图像: " + vouImg.substring(0, 300));
				// 有图像，调用WebService存储
				IImageService iis = new ImageServiceImpl();
				String imgXML = XmlParseUtil.markImageServXML("", vouImg);
				log.debug("2201 saveImage XML: " + imgXML);
				String imgid = iis.saveImage(2201, imgXML);
				voucher.setFimgid(imgid);
			}
		}

		baseDAO.saveOrUpdate(voucher);
		return voucher.getFimgid();
	}
}
